#include "StdAfx.h"
#include "GameClient.h"

#include "../network/NetMgr.h"


CGameClient::CGameClient(CNetMgr *netMgr, int clientID, BOOL local)
{
	m_netMgr = netMgr;
	m_clientID = clientID;
	m_local = local;

	for (int i=0;i<MAX_COMMANDS;i++)
		m_commandUsed[i] = FALSE;
}

CGameClient::~CGameClient()
{
}

CGameCommand *CGameClient::readCommand(int time)
{
	for (int i=0;i<MAX_COMMANDS;i++)
	{
		if (m_commandUsed[i] && (m_commands[i].getTime() == time))
			return &m_commands[i];
	}
	return NULL;
}

BOOL CGameClient::writeCommand(CGameCommand *command)
{
	return m_netMgr->broadcastCommand(command);
}

BOOL CGameClient::pushCommand(CGameCommand *command)
{
	for (int i=0;i<MAX_COMMANDS;i++)
	{
		if (!m_commandUsed[i])
		{
			m_commands[i] = *command;
			m_commandUsed[i] = TRUE;
			return TRUE;
		}
	}

	return FALSE;
}

void CGameClient::flushCommands(int time)
{
	for (int i=0;i<MAX_COMMANDS;i++)
	{
		if (m_commandUsed[i] && (m_commands[i].getTime() <= time))
			m_commandUsed[i] = FALSE;
	}
}

